package com.gzbugu.bugusys.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionSupport;
import com.gzbugu.bugusys.DTO.StaffDTO;
import com.gzbugu.bugusys.vo.Staff;
import com.gzbugu.bugusys.vo.StaffEducation;
import com.gzbugu.bugusys.vo.StaffCertificate;
import com.gzbugu.bugusys.vo.StaffExperience;
import com.gzbugu.bugusys.vo.StaffFamily;
import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.fields.DatabaseFields;
import com.gzbugu.bugusys.util.HibernateUtils;

public class AddStaffAction extends ActionSupport {
	private String url;
	private File photo;
	private String photoFileName;
	private String photoContentType;
	private StaffDTO staffDTO;
	private String staffId;
	private List errorList = null;
	Integer staffSex;
	StaffFamily staffFamily1, staffFamily2, staffFamily3;
	StaffEducation staffEducation1, staffEducation2, staffEducation3;
	StaffExperience staffExperience1, staffExperience2, staffExperience3;
	StaffCertificate staffCertificate1, staffCertificate2, staffCertificate3;

	@Resource(name = "databaseFields")
	DatabaseFields fields;
	@Resource(name = "baseDAO")
	IBaseDAO baseDAO;

	@Transactional
	public String execute() {
		errorList = new ArrayList();
		staffDTO.setError(errorList);
		String result = "";
		if (staffDTO.invalidate()) {

			// 将各个表格列属性赋值，首先是staffSex
			if (staffDTO.getStaffSexId() == 1) {
				// staffSex = new StaffSex();
				// staffSex.setStaffSexId(staffDTO.getStaffSexId());
				// staffSex.setStaffSexName("男");
				staffSex = 2;
			} else {
				// staffSex = new StaffSex();
				// staffSex.setStaffSexId(staffDTO.getStaffSexId());
				// staffSex.setStaffSexName("女");
				staffSex = 3;
			}
			if (photo != null) {
				try {
					String root = ServletActionContext.getRequest()
							.getRealPath("/upload/photo");
					String fileName = new Date().getTime()
							+ this.getExtention(getPhotoFileName());
					this.setUrl("upload/photo/" + fileName);
					File target = new File(root, fileName);
					FileUtils.copyFile(photo, target);
				} catch (Exception e) {
					result = INPUT;
				}

			}
			Staff staff = new Staff();
			if (staffId != null && !"".equals(staffId)) {
				staff = (Staff) baseDAO.findById(staffId,
						com.gzbugu.bugusys.fields.VoClasseFields.STAFF);
				clear(staff);
			}
			staff.setStaffId(staffDTO.getStaffId());
			staff.setName(staffDTO.getStaffName());
			staff.setAddress(staffDTO.getStaffAddress());
			staff.setBirthday(staffDTO.getStaffBirthday());
			staff.setBirthplace(staffDTO.getStaffBirthplace());
			staff.setFormername(staffDTO.getStaffFormername());
			staff.setDepartment(staffDTO.getStaffDepartment());
			staff.setEducation(staffDTO.getStaffEducation());
			staff.setGraduateSchool(staffDTO.getStaffGraduateSchool());
			staff.setHiredate(staffDTO.getStaffHiredate());
			staff.setIdnumber(staffDTO.getStaffIdnumber());
			staff.setNation(staffDTO.getStaffNation());
			staff.setNativePlace(staffDTO.getStaffNativePlace());
			staff.setPoliticsStatus(staffDTO.getStaffPoliticsStatus());
			staff.setPost(staffDTO.getStaffPost());
			staff.setTel(staffDTO.getStaffTel());
			staff.setZip(staffDTO.getStaffZip());
			staff.setSex(staffSex);
			if (url != null) {
				staff.setPhotoUrl(url);
			}
			baseDAO.attachDirty(staff);

			if (!("".equals(staffDTO.getStaffEducationEnd1())
					|| "".equals(staffDTO.getStaffEducationStart1())
					|| "".equals(staffDTO.getStaffEducationSchool1()) || ""
					.equals(staffDTO.getStaffEducationReference1()))) {
				staffEducation1 = new StaffEducation();
				staffEducation1.setEnd(staffDTO.getStaffEducationEnd1());
				staffEducation1.setStart(staffDTO.getStaffEducationStart1());
				staffEducation1.setSchool(staffDTO.getStaffEducationSchool1());
				staffEducation1.setReference(staffDTO
						.getStaffEducationReference1());
				staffEducation1.setStaff(staff);
				baseDAO.save(staffEducation1);
			}

			if (!("".equals(staffDTO.getStaffEducationEnd2())
					|| "".equals(staffDTO.getStaffEducationStart2())
					|| "".equals(staffDTO.getStaffEducationSchool2()) || ""
					.equals(staffDTO.getStaffEducationReference2()))) {
				staffEducation2 = new StaffEducation();
				staffEducation2.setEnd(staffDTO.getStaffEducationEnd2());
				staffEducation2.setStart(staffDTO.getStaffEducationStart2());
				staffEducation2.setSchool(staffDTO.getStaffEducationSchool2());
				staffEducation2.setReference(staffDTO
						.getStaffEducationReference2());
				staffEducation2.setStaff(staff);
				baseDAO.save(staffEducation2);
			}

			if (!("".equals(staffDTO.getStaffEducationEnd3())
					|| "".equals(staffDTO.getStaffEducationStart3())
					|| "".equals(staffDTO.getStaffEducationSchool3()) || ""
					.equals(staffDTO.getStaffEducationReference3()))) {
				staffEducation3 = new StaffEducation();
				staffEducation3.setEnd(staffDTO.getStaffEducationEnd3());
				staffEducation3.setStart(staffDTO.getStaffEducationStart3());
				staffEducation3.setSchool(staffDTO.getStaffEducationSchool3());
				staffEducation3.setReference(staffDTO
						.getStaffEducationReference3());
				staffEducation3.setStaff(staff);
				baseDAO.save(staffEducation3);
			}

			if (!("".equals(staffDTO.getStaffExperienceEnd1())
					|| staffDTO.getStaffExperiencePost1() == null
					|| staffDTO.getStaffExperienceStart1() == null || ""
					.equals(staffDTO.getStaffExperienceUnit1()))) {
				staffExperience1 = new StaffExperience();
				staffExperience1.setStart(staffDTO.getStaffExperienceStart1());
				staffExperience1.setEnd(staffDTO.getStaffExperienceEnd1());
				staffExperience1.setPost(staffDTO.getStaffExperiencePost1());
				staffExperience1.setUnit(staffDTO.getStaffExperienceUnit1());
				staffExperience1.setStaff(staff);
				baseDAO.save(staffExperience1);
			}

			if (!("".equals(staffDTO.getStaffExperienceEnd2())
					|| staffDTO.getStaffExperiencePost2() == null
					|| staffDTO.getStaffExperienceStart2() == null || ""
					.equals(staffDTO.getStaffExperienceUnit2()))) {
				staffExperience2 = new StaffExperience();
				staffExperience2.setStart(staffDTO.getStaffExperienceStart2());
				staffExperience2.setEnd(staffDTO.getStaffExperienceEnd2());
				staffExperience2.setPost(staffDTO.getStaffExperiencePost2());
				staffExperience2.setUnit(staffDTO.getStaffExperienceUnit2());
				staffExperience2.setStaff(staff);
				baseDAO.save(staffExperience2);
			}

			if (!("".equals(staffDTO.getStaffExperienceEnd3())
					|| staffDTO.getStaffExperiencePost3() == null
					|| staffDTO.getStaffExperienceStart3() == null || ""
					.equals(staffDTO.getStaffExperienceUnit3()))) {
				staffExperience3 = new StaffExperience();
				staffExperience3.setStart(staffDTO.getStaffExperienceStart3());
				staffExperience3.setEnd(staffDTO.getStaffExperienceEnd3());
				staffExperience3.setPost(staffDTO.getStaffExperiencePost3());
				staffExperience3.setUnit(staffDTO.getStaffExperienceUnit3());
				staffExperience3.setStaff(staff);
				baseDAO.save(staffExperience3);
			}

			if (!("".equals(staffDTO.getStaffFamilyName1())
					|| "".equals(staffDTO.getStaffFamilyPost1())
					|| "".equals(staffDTO.getStaffFamilyRelationship1()) || ""
					.equals(staffDTO.getStaffFamilyUnit1()))) {
				staffFamily1 = new StaffFamily();
				staffFamily1.setName(staffDTO.getStaffFamilyName1());
				staffFamily1.setRelationship(staffDTO
						.getStaffFamilyRelationship1());
				staffFamily1.setPost(staffDTO.getStaffFamilyPost1());
				staffFamily1.setUnit(staffDTO.getStaffFamilyUnit1());
				staffFamily1.setStaff(staff);
				baseDAO.save(staffFamily1);
			}

			if (!("".equals(staffDTO.getStaffFamilyName2())
					|| "".equals(staffDTO.getStaffFamilyPost2())
					|| "".equals(staffDTO.getStaffFamilyRelationship2()) || ""
					.equals(staffDTO.getStaffFamilyUnit2()))) {
				staffFamily2 = new StaffFamily();
				staffFamily2.setName(staffDTO.getStaffFamilyName2());
				staffFamily2.setRelationship(staffDTO
						.getStaffFamilyRelationship2());
				staffFamily2.setPost(staffDTO.getStaffFamilyPost2());
				staffFamily2.setUnit(staffDTO.getStaffFamilyUnit2());
				staffFamily2.setStaff(staff);
				baseDAO.save(staffFamily2);
			}

			if (!("".equals(staffDTO.getStaffFamilyName3())
					|| "".equals(staffDTO.getStaffFamilyPost3())
					|| "".equals(staffDTO.getStaffFamilyRelationship3()) || ""
					.equals(staffDTO.getStaffFamilyUnit3()))) {
				staffFamily3 = new StaffFamily();
				staffFamily3.setName(staffDTO.getStaffFamilyName3());
				staffFamily3.setRelationship(staffDTO
						.getStaffFamilyRelationship3());
				staffFamily3.setPost(staffDTO.getStaffFamilyPost3());
				staffFamily3.setUnit(staffDTO.getStaffFamilyUnit3());
				staffFamily3.setStaff(staff);
				baseDAO.save(staffFamily3);
			}

			if (!(staffDTO.getStaffCertificateIssueDate1() == null
					|| staffDTO.getStaffCertificateNotafter1() == null
					|| staffDTO.getStaffCertificateNotbefore1() == null || ""
					.equals(staffDTO.getStaffCertificateName1()))) {
				staffCertificate1 = new StaffCertificate();
				staffCertificate1.setName(staffDTO.getStaffCertificateName1());
				staffCertificate1.setIssueDate(staffDTO
						.getStaffCertificateIssueDate1());
				staffCertificate1.setNotafter(staffDTO
						.getStaffCertificateNotafter1());
				staffCertificate1.setNotbefore(staffDTO
						.getStaffCertificateNotbefore1());
				staffCertificate1.setStaff(staff);
				baseDAO.save(staffCertificate1);
			}

			if (!(staffDTO.getStaffCertificateIssueDate2() == null
					|| staffDTO.getStaffCertificateNotafter2() == null
					|| staffDTO.getStaffCertificateNotbefore2() == null || ""
					.equals(staffDTO.getStaffCertificateName2()))) {
				staffCertificate2 = new StaffCertificate();
				staffCertificate2.setName(staffDTO.getStaffCertificateName2());
				staffCertificate2.setIssueDate(staffDTO
						.getStaffCertificateIssueDate2());
				staffCertificate2.setNotafter(staffDTO
						.getStaffCertificateNotafter2());
				staffCertificate2.setNotbefore(staffDTO
						.getStaffCertificateNotbefore2());
				staffCertificate2.setStaff(staff);
				baseDAO.save(staffCertificate2);
			}

			if (!(staffDTO.getStaffCertificateIssueDate3() == null
					|| staffDTO.getStaffCertificateNotafter3() == null
					|| staffDTO.getStaffCertificateNotbefore3() == null || ""
					.equals(staffDTO.getStaffCertificateName3()))) {
				staffCertificate3 = new StaffCertificate();
				staffCertificate3.setName(staffDTO.getStaffCertificateName3());
				staffCertificate3.setIssueDate(staffDTO
						.getStaffCertificateIssueDate3());
				staffCertificate3.setNotafter(staffDTO
						.getStaffCertificateNotafter3());
				staffCertificate3.setNotbefore(staffDTO
						.getStaffCertificateNotbefore3());
				staffCertificate3.setStaff(staff);
				baseDAO.save(staffCertificate3);
			}
			result = "success";
		} else {
			result = "cuowu";
		}
		return result;

	}

	@SuppressWarnings("unchecked")
	public void clear(Staff staff) {
		Set certificates = staff.getStaffCertificates();
		Set documents = staff.getStaffDocumentses();
		Set experiences = staff.getStaffExperiences();
		Set families = staff.getStaffFamilies();
		Set educations = staff.getStaffEducations();
		staff.setStaffCertificates(null);
		staff.setStaffDocumentses(null);
		staff.setStaffEducations(null);
		staff.setStaffExperiences(null);
		staff.setStaffFamilies(null);
		clear(certificates);
		clear(documents);
		clear(experiences);
		clear(families);
		clear(educations);
	}

	@SuppressWarnings("unchecked")
	public void clear(Set set) {
		for (Object obj : set) {
			baseDAO.delete(obj);
		}
	}

	public String getExtention(String filename) {
		int index = filename.lastIndexOf(".");
		return filename.substring(index);
	}

	public StaffDTO getStaffDTO() {
		return staffDTO;
	}

	public void setStaffDTO(StaffDTO staffDTO) {
		this.staffDTO = staffDTO;
	}

	public List getErrorList() {
		return errorList;
	}

	public void setErrorList(List errorList) {
		this.errorList = errorList;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public String getStaffId() {
		return staffId;
	}

}
